"use strict";
const Controller = require("egg").Controller;
const tokenUtil = require("../utils/token");
const errorLog = require("../utils/errorLog");

function toInt(str) {
  if (typeof str === "number") return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
}

class UserController extends Controller {
  async test() {
    this.ctx.body = "test";
  }

  async index() {
    const ctx = this.ctx;
    const query = {
      limit: toInt(ctx.query.limit),
      offset: toInt(ctx.query.offset),
    };
    ctx.body = await ctx.model.User.findAll(query);
  }

  async login() {
    try {
      const { account, password } = this.ctx.request.body;
      const user = await this.ctx.service.user.login(account, password);
      if (user) {
        const role = {
          roleId: user.roleId,
          roleName: user.role.dataValues.roleName,
          roleRoute: user.role.dataValues.roleRoute,
        };
        const token = tokenUtil.generateToken({
          userId: user.id,
          roleId: user.roleId,
        });
        this.ctx.body = {
          code: 200,
          msg: "登录成功",
          data: { user, token, role },
        };
      } else {
        this.ctx.body = { code: 500, msg: "账号或密码错误" };
      }
    } catch (err) {
      errorLog("userController----->login", err);
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
    }
  }

  async createUser() {
    try {
      const { userObj } = this.ctx.request.body;
      const user = await this.ctx.service.user.createUser(userObj);
      if (user) {
        if (user.msg) {
          this.ctx.body = { code: user.code, msg: user.msg };
          return;
        }
        this.ctx.body = { code: 200, msg: "添加成功" };
      } else {
        this.ctx.body = { code: 500, msg: "添加失败" };
      }
    } catch (err) {
      errorLog("userController----->createUser", err);
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
    }
  }

  async deleteUser() {
    try {
      const { id } = this.ctx.request.body;
      const user = await this.ctx.service.user.deleteUser(id);
      if (user) {
        this.ctx.body = { code: 200, msg: "删除成功" };
      } else {
        this.ctx.body = { code: 500, msg: "删除失败" };
      }
    } catch (err) {}
  }

  async getUserList() {
    try {
      const { currentPage, pageSize } = this.ctx.request.body;
      const userList = await this.ctx.service.user.getUserList(
        currentPage,
        pageSize
      );
      if (userList) {
        this.ctx.body = { code: 200, msg: "获取成功", data: userList };
      } else {
        this.ctx.body = { code: 500, msg: "获取失败" };
      }
    } catch (err) {
      errorLog("userController----->getUserList", err);
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
    }
  }

  async updateUser() {
    try {
      const requestRoleId = this.ctx.state.user.roleId;
      const { id, userObj } = this.ctx.request.body;
      console.log(requestRoleId, id);
      if (requestRoleId !== 3 && id === 1) {
        this.ctx.body = {
          code: 500,
          msg: "只有超级管理员能够更新这个账号",
        };
        return;
      }

      const user = await this.ctx.service.user.updateUser(id, userObj);
      if (user) {
        if (user.msg) {
          this.ctx.body = { code: user.code, msg: user.msg };
        } else {
          this.ctx.body = { code: 200, msg: "修改成功" };
        }
      } else {
        this.ctx.body = { code: 500, msg: "修改失败" };
      }
    } catch (err) {
      errorLog("userController----->updateUser", err);
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
    }
  }

  async deleteUser() {
    try {
      const { id } = this.ctx.params;
      if (id == 1) {
        this.ctx.body = { code: 500, msg: "这个账号不能删除" };
        return false;
      }
      const user = await this.ctx.service.user.deleteUser(id);
      if (user) {
        this.ctx.body = { code: 200, msg: "删除成功" };
      } else {
        this.ctx.body = { code: 500, msg: "删除失败" };
      }
    } catch (err) {
      errorLog("userController----->deleteUser", err);
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
    }
  }

  async updateUserPasswordById() {
    try {
      const requestRoleId = this.ctx.state.user.roleId;
      const userObj = this.ctx.request.body;
      if (requestRoleId !== 3 && userObj.id === 1) {
        this.ctx.body = {
          code: 500,
          msg: "只有超级管理员能够更新这个账号",
        };
        return;
      }
      const user = await await this.ctx.service.user.updateUserPasswordById(
        userObj
      );
      if (user) {
        if (user.msg) {
          this.ctx.body = { code: user.code, msg: user.msg };
        } else {
          this.ctx.body = { code: 200, msg: "修改成功" };
        }
      } else {
        this.ctx.body = { code: 500, msg: "修改失败" };
      }
    } catch (err) {
      errorLog("userController----->updateUserPasswordById", err);
      this.ctx.body = { code: 500, msg: "服务器发生错误" };
    }
  }
}

module.exports = UserController;
